package jobs;

import jobs.annotations.JobInfo;
import org.apache.commons.lang.StringUtils;
import play.Logger;
import play.Play;
import play.jobs.Job;
import utils.ShortUUID;
import utils.UUIDUtils;
import utils.xtask.XTaskService;

import java.util.Date;

/**
 * @author zhangzc
 * @since 2017/7/10
 */
public abstract  class BaseJob extends Job {

    private String jobId;

    @Override
    public void doJob(){
        JobInfo jobInfo = this.getClass().getAnnotation(JobInfo.class);
        if(jobInfo == null){
//            Logger.info("Job[%s] executed at: %s", new Date());
            this._executeJob();
        }

        String jobName = jobInfo.name();
        if(StringUtils.isEmpty(jobName)){
            Logger.info("Job[%s] executed at: %s", jobName, new Date());
            this._executeJob();
        }

        String flag = Play.configuration.getProperty("job."+jobName, "enable");
        if(StringUtils.equalsIgnoreCase(flag, "enable")){
            Logger.info("Job[%s] executed at: %s", jobName, new Date());
            this._executeJob();
        }
    }



    private void _executeJob(){
        beforeExecute();
        try{
            executeJob();
        }catch (Throwable e){
            onError(e);
        }finally {
            afterExcecute();
        }


    }

    private void beforeExecute(){
        this.jobId = ShortUUID.generate();
        long startExecuteTime = System.currentTimeMillis();
        String jobName = this.getClass().getName();
        XTaskService.saveTime(jobName, "start", startExecuteTime);
        XTaskService.saveExecuteCount(jobName);
        XTaskService.saveLog(jobName, this.jobId, "开始");
    }

    private void afterExcecute(){
        long endExecuteTime = System.currentTimeMillis();
        String jobName = this.getClass().getName();
        XTaskService.saveTime(this.getClass().getName(), "end", endExecuteTime);
        XTaskService.saveLogProgress(jobName, this.jobId, 100);
    }

    private void onError(Throwable e){
        Logger.error(e, "Job Error!");
        logError(e, "异常！");
    }

    protected void logInfo(String content, Object... objects){
        Logger.info(content, objects);
        String jobName = this.getClass().getName();
        XTaskService.saveLog(jobName, this.jobId, String.format(content, objects));
    }

    protected void logWarn(String content, Object... objects){
        Logger.warn(content, objects);
        String jobName = this.getClass().getName();
        XTaskService.saveLog(jobName, this.jobId, "WARN", String.format(content, objects));
    }

    protected void logError(String content, Object... objects){
        Logger.error(content, objects);
        String jobName = this.getClass().getName();
        XTaskService.saveLog(jobName, this.jobId, "ERROR", String.format(content, objects));
    }

    protected void logError(Throwable ex, String content, Object... objects){
        Logger.error(ex,content, objects);
        String jobName = this.getClass().getName();
        XTaskService.saveLog(jobName, this.jobId, "ERROR", String.format(content, objects), ex);
    }

    protected void setJobProgress(int progress){
        String jobName = this.getClass().getName();
        XTaskService.saveLogProgress(jobName, this.jobId, progress);
    }

    public abstract void executeJob();


}
